package com.activitycode.main;



import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.activitycode.adapter.FunctionAdapter;
import com.activitycode.adapter.FunctionAdapter.CallOnClickListener;
import com.activitycode.adapter.HomeAdapter;
import com.activitycode.datamodel.modelinfo.HomeItemInfo;
import com.activitycode.im.EimApplication;
import com.activitycode.tools.PublicTools;
import com.activitycode.util.DataBaseHelper;
import com.activitycode.view.AutoScrollTextView;
import com.duostec.zhuyue.taxis.R;
import com.j256.ormlite.dao.Dao;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;



public class HomeFunctionActivity extends Activity implements OnClickListener{
	private ImageButton ivTitleBtnLeft;
	private TextView titleText,tipText;
	private ListView pagerGridView;
	private AutoScrollTextView autoText;
	private List<HomeItemInfo> itemInfoList = new ArrayList<HomeItemInfo>();
	private FunctionAdapter adapter;
	private DataBaseHelper dataBaseHelper;
	private Dao<HomeItemInfo, String> homeItemInfoDao;
	
	
	private EimApplication eimApp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_home_function);
		
		eimApp = (EimApplication) getApplication();
		initView();
		initData();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		queryFunction();
	}
	
	private void initView(){
		ivTitleBtnLeft = (ImageButton) this.findViewById(R.id.title_back_btn);
		ivTitleBtnLeft.setOnClickListener(this);
		pagerGridView = (ListView) this.findViewById(R.id.functionlist_listview);
		titleText = (TextView) this.findViewById(R.id.title_back_title);
		tipText = (TextView) this.findViewById(R.id.functionlist_tips);
		titleText.setText(""+getResources().getString(R.string.function_tip3));
	}
	
	private void initData(){
		
		dataBaseHelper = new DataBaseHelper(this);
		try {
			homeItemInfoDao = dataBaseHelper.getHomeItemInfoDao();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		adapter = new FunctionAdapter(this,itemInfoList);
		adapter.setCallOnClickListener(new CallOnClickListener(){

			@Override
			public void onClick(int position) {
				// TODO Auto-generated method stub
				try {
					itemInfoList.get(position).setStatus("1");
					try {
						homeItemInfoDao.createOrUpdate(itemInfoList.get(position));
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finish();
				} catch (Exception e) {
					// TODO: handle exception
				}
				
			}
			
		});
		pagerGridView.setAdapter(adapter);
		queryFunction();
		if(itemInfoList.size()<=0){
			tipText.setVisibility(View.VISIBLE);
		}
	}
	
	
	
	private void queryFunction(){
		try {
			List<HomeItemInfo> list = homeItemInfoDao.queryBuilder().orderBy("sort", true).where().eq("status", "0").query();
			if(list!=null&&list.size()>0){
				itemInfoList.clear();
				for(int i=0;i<list.size();i++){
					itemInfoList.add(list.get(i));
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.title_back_btn:
			finish();
			break;
		}
	}
	
	
	
	
	
	
	
}